/**
*   FuzzyEngine - an Simple flex framework 
*   Copyright (C) 2010 Constantin Aldea
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package fuzzy.penelope.model {
	import fuzzy.penelope.model.errors.interfaces.IErrorManager;
	import fuzzy.penelope.model.errors.interfaces.IServiceError;
	import fuzzy.penelope.model.vo.framework.exceptions.ServiceException;
	import fuzzy.penelope.model.vo.framework.exceptions.SystemException;

	import mx.controls.Alert;
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.utils.ObjectUtil;

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	[ExcludeClass]
	public class ErrorManagerImpl extends EventDispatcher implements IErrorManager
	{


		public static const MSG_ATTENZIONE:String="Attenzione.";

		public function ErrorManagerImpl(target:IEventDispatcher=null)
		{
			super(target);
			log  = Log.getLogger("com.fuzzy.model.ErrorManagerImpl");
		
		}
		
		
		public function localMessenger(msg:String):void{
			
		}
				private var _log:ILogger;

		private var errorAlert:Alert;

		/**
		 * 
		 * @param msg
		 * @description Gestione centralizzata degli errori, invecce di usare 
		 * varie volte Alert.show 
		 * <code>ErrorManager.getInstace().localErrorMessage(" non puo fare l'operazione " );</code>
		 */
		public function localErrorMessage(msg:String):void
		{
			errorAlert=Alert.show(msg, ErrorManagerImpl.MSG_ATTENZIONE);
		}

		public function get log():ILogger
		{
			return _log;
		}

		public function set log(val:ILogger):void
		{
			_log = val;
		}
		/**
		 * 
		 * @param e
		 * @Gestisce gli errori venute dal server, nel caso di un fault event
		 */		
		public function remoteError(e:FaultEvent):void
		{
			var msg:String = e.message + ": id "+e.messageId;
			errorAlert=Alert.show(msg, ErrorManagerImpl.MSG_ATTENZIONE);
			log.log(LogEventLevel.ALL, msg);
		}

		/**
		 * 
		 * @param e
		 * @Gestione centrale degli errori dei servizi/system di fuzzy 
		 */
		public function serviceError(e:IServiceError):void
		{
			if (e is SystemException)
			{
				errorAlert=Alert.show((e as SystemException).detailMessage, ErrorManagerImpl.MSG_ATTENZIONE);
				log.log(LogEventLevel.ALL, (e as SystemException).detailMessage);
			}
			else if (e is ServiceException)
			{
				errorAlert=Alert.show((e as ServiceException).detailMessage, ErrorManagerImpl.MSG_ATTENZIONE);
				log.log(LogEventLevel.ALL, (e as ServiceException).detailMessage);
			}
			else
			{
				errorAlert=Alert.show("Errore di sistema, probabilmente la risposta del server e null", ErrorManagerImpl.MSG_ATTENZIONE);
				log.log(LogEventLevel.ALL, ObjectUtil.toString(e));
			}
			
			CursorManager.removeAllCursors();
			Context.getInstance().contextLoader.removeLoading();

		}
	}
}